package controllers

import (
	"blog-backend/internal/app/dtos"
	"blog-backend/internal/app/services"
	"blog-backend/internal/utils"
	"net/http"

	"github.com/gin-gonic/gin"
)

type TagController struct {
	tagService *services.TagService
}

func NewTagController() *TagController {
	return &TagController{
		tagService: services.NewTagService(),
	}
}

// GetTags 获取所有标签
// @Summary 获取所有标签
// @Description 获取所有标签列表
// @Tags 标签
// @Produce json
// @Success 200 {object} dtos.SuccessResponse{data=dtos.TagListResponse} "获取成功"
// @Failure 500 {object} dtos.ErrorResponse "服务器内部错误"
// @Router /api/tags [get]
func (c *TagController) GetTags(ctx *gin.Context) {
	tags, err := c.tagService.GetTags()
	if err != nil {
		utils.RespondError(ctx, http.StatusInternalServerError, err.Error())
		return
	}

	// 构建响应DTO
	var tagInfos []dtos.TagInfo
	for _, tag := range tags {
		tagInfos = append(tagInfos, dtos.TagInfo{
			ID:   tag.ID,
			Name: tag.Name,
			Slug: tag.Slug,
		})
	}

	utils.RespondJSON(ctx, http.StatusOK, dtos.SuccessResponse{
		Success: true,
		Message: "获取标签列表成功",
		Data: dtos.TagListResponse{
			Tags:  tagInfos,
			Total: len(tagInfos),
		},
	})
}
